/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.dto.mapping;

import cz.muni.fi.pa165.creatures.dto.CreatureDTO;
import cz.muni.fi.pa165.creatures.dto.mapping.api.MappingDTO;
import cz.muni.fi.pa165.creatures.model.Creature;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * This class represents the mapping between
 * {@link cz.muni.fi.pa165.creatures.model.Creature} and
 * {@link cz.muni.fi.pa165.creatures.dto.CreatureDTO} and vice versa.
 *
 * @author smikloso
 */
public class CreatureMapping implements MappingDTO<CreatureDTO, Creature> {

    @Autowired
    RegionMapping regionMapping;

    @Override
    public CreatureDTO entityToDTO(Creature creature) {
        CreatureDTO dto = new CreatureDTO();
        dto.setId(creature.getId().toString());
        dto.setName(creature.getName());
        dto.setWeight(creature.getWeight().toString());
        dto.setHeight(creature.getHeight().toString());
        dto.setActualHitPoints(creature.getActualHitPoints().toString());

        if (creature.getRegion() != null && creature.getRegion().getName().length() > 0) {
            dto.setRegionDTO(regionMapping.entityToDTO(creature.getRegion()));
        }

        return dto;
    }

    @Override
    public Creature DTOtoEntity(CreatureDTO dto) {
        Creature creature = new Creature();
        creature.setId(Long.parseLong(dto.getId()));
        creature.setName(dto.getName());
        creature.setHeight(Integer.parseInt(dto.getHeight()));
        creature.setWeight(Integer.parseInt(dto.getWeight()));
        creature.setActualHitPoints(Integer.parseInt(dto.getActualHitPoints()));

        if (dto.getRegionDTO() != null) {
            creature.setRegion(regionMapping.DTOtoEntity(dto.getRegionDTO()));
        }
        
        return creature;
    }
}
